//
//  SearchViewViewController.m
//  TheRoadAD
//
//  Created by baixue on 16/9/10.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "SearchViewViewController.h"
#import "SearchCell.h"
#import "ResultViewController.h"

@interface SearchViewViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITextField *_resultTF;
    UIImageView *_sousuoImg;
    
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;


@end

@implementation SearchViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self setNav];
    [self setTableView];
    
    _dataArray = [NSMutableArray array];
    
}

//设置导航按钮 搜索
- (void)setNav{

    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(40, 5, Screen_Width-90, 30)];
    [self.navigationController.navigationBar addSubview:image];
    image.image = [UIImage imageNamed:@"sousuo"];
    _sousuoImg = image;
    
    UITextField *TF = [[UITextField alloc] initWithFrame:CGRectMake(50, 10, Screen_Width-90, 20)];
    [self.navigationController.navigationBar addSubview:TF];
    TF.placeholder = @"静态搜索";
    TF.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_home_meitu_2"]];
    TF.leftViewMode = UITextFieldViewModeAlways;
    _resultTF = TF;


    
    //导航右编辑按钮
    UIBarButtonItem *search = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:0 target:self action:@selector(searchItem)];
    self.navigationItem.rightBarButtonItem = search;
    
}

-(void)searchItem{
    
//    [_dataArray addObject:_resultTF];
    
    ResultViewController *result = [[ResultViewController alloc] init];
    [self.navigationController pushViewController:result animated:YES];
    
   [[NSUserDefaults standardUserDefaults] setObject:_dataArray forKey:@"acv"];
    
    
}


- (void)setTableView{
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    _tableView.bounces = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = 0;
    [self.view addSubview:_tableView];
    //注册
    [_tableView registerNib:[UINib nibWithNibName:@"SearchCell" bundle:nil] forCellReuseIdentifier:@"SearchCell"];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_dataArray.count == 0) {
        return 1;
    }
    
    return _dataArray.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SearchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchCell"];
    if (_dataArray.count == 0) {
        cell.dataList.text = @" 暂无搜索记录";
    }else{
        
        NSArray *resultArr = [[NSUserDefaults standardUserDefaults] objectForKey:@"acv"];
        cell.dataList.text = resultArr[indexPath.row];
    }
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 60)];
    headerView.backgroundColor = [UIColor whiteColor];
    UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, Screen_Width-20, 59)];
    lable.text = @"历史搜索";
    lable.font = [UIFont systemFontOfSize:15.0f];
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 59, Screen_Width-20, 1)];
    image.image = [UIImage imageNamed:@"xian"];
    [headerView addSubview:lable];
    [headerView addSubview:image];
    
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 60;
}


// footer按钮
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, Screen_Height/2, Screen_Width, Screen_Height/2)];
    footerView.backgroundColor = [UIColor whiteColor];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(20,70, Screen_Width-40, 50 * Screen_Height/736)];
    [footerView addSubview:button];
    [button setBackgroundImage:[UIImage imageNamed:@"icon_home_find_4"] forState:0];
    [button setTitle:@"清除搜索记录" forState:0];
    [button setTitleColor:[UIColor blackColor] forState:0];
    button.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    return footerView;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 130;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [_sousuoImg removeFromSuperview];
    [_resultTF removeFromSuperview];
}

@end
